package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbStageDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbStage;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbStageCustomWhere;
import com.ds.infrastructure.hyperspace.console.exceptions.ArgumentValidException;
import com.ds.infrastructure.hyperspace.console.service.inf.IStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: yaozhendong
 * @create: 2019-09-20 09:19
 **/

@Service
public class StageService implements IStageService {
    @Autowired
    TbStageDao tbStageDao;

    @Override
    public int save(TbStage record) {
        return tbStageDao.insertSelective(record);
    }

    @Override
    public int update(TbStage record) {
        return tbStageDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbStage get(Long id) {
        TbStage entity = tbStageDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbStage item = new TbStage();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbStage> getList(TbStage where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbStageDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbStage where, TbCustomWhere customWhere) {
        return tbStageDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbStage selectOne(TbStage where, TbCustomWhere customWhere) {
        List<TbStage> itemList = tbStageDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    /**
     * 检查并获得阶段
     *
     * @param id
     * @return
     */
    @Override
    public TbStage getOrThrow(Long id) {
        return getOrThrow(id, null);
    }

    @Override
    public TbStage getOrThrow(Long id, String exceptionTitle) {

        TbStage tbStageEntity = get(id);
        if (tbStageEntity == null) {
            String error = String.format("%s:没有找到对应的记录;当前id=%s", exceptionTitle == null ? "获取阶段信息失败" : exceptionTitle, id);
            throw new ArgumentValidException(error);
        }
        return tbStageEntity;
    }

    /**
     * 获得阶段名称与ID的映射集合
     *
     * @param stageIdList
     * @return
     */
    @Override
    public Map<Long, String> getTbStageMap(List<Long> stageIdList) {

        TbStageCustomWhere customWhere = new TbStageCustomWhere().setIdList4In(stageIdList);
        List<TbStage> tbStageList = getList(null, customWhere, TbPageWhere.newPage(0, stageIdList.size()));
        if (tbStageList.isEmpty()) {
            return Collections.emptyMap();
        }
        return tbStageList.stream().collect(Collectors.toMap(TbStage::getId, TbStage::getStageName));
    }

    @Override
    public TbStage getAndSave(String stageName, Long environmentId) {
        TbStage record = new TbStage();
        record.setStageName(stageName);
        record.setEnvironmentId(environmentId);
        record.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        TbStage item4Select = selectOne(record, null);
        if (item4Select != null) {
            return item4Select;
        }
        save(record);
        return record;
    }

    @Override
    public List<TbStage> getAllByEnvironmentIds(List<Long> environmentIds) {
        TbStage where = new TbStage();
        where.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);

        TbStageCustomWhere customWhere = new TbStageCustomWhere();
        return getList(where, customWhere, TbPageWhere.newPage4All());

    }
}
